﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Innova.Microfinanzas.BusinessEntities;
using Innova.Microfinanzas.BusinessEntities.EnumClassifierTypes;

namespace MyCommerce.Business.GeneralParameters
{
    public class CommonClassifierManager
    {
        private MyCommerceEntities vContext;

        public CommonClassifierManager()
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = false;
            }
        }

        public CommonClassifierManager(bool lazyLoad)
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = lazyLoad;
            }
        }

        /// <summary>
        /// Obtiene los clasificadores donde el tipo de clasificador NO es de sistema.
        /// </summary>
        /// <returns></returns>
        public List<Classifier> GetListClassifier()
        {
            List<Classifier> colClassifier = new List<Classifier>();
            var classifierResult = from cl in vContext.Classifier
                                   join clt in vContext.ClassifierType on cl.IdClassifierType equals clt.IdClassifierType
                                   where clt.IsSystem == false
                                   select cl;

            if (classifierResult != null)
	        {
                colClassifier = new List<Classifier>(classifierResult.AsEnumerable<Classifier>());
	        }

            return colClassifier;
        }

        public List<Classifier> GetListClassifier(bool isActive)
        {
            List<Classifier> colClassifier = new List<Classifier>();
            var classifierResult = from cl in vContext.Classifier
                                   join clt in vContext.ClassifierType on cl.IdClassifierType equals clt.IdClassifierType
                                   where cl.IsActive == isActive && clt.IsSystem == false
                                   select cl;

            if (classifierResult != null)
            {
                colClassifier = new List<Classifier>(classifierResult.AsEnumerable<Classifier>());
            }

            return colClassifier;
        }

        /// <summary>
        /// Obtiene clasificadores segun su ID
        /// </summary>
        /// <param name="IdClassifier"></param>
        /// <returns></returns>
        public Classifier GetClassifierByID(long IdClassifier)
        {
            Classifier objClassifier = new Classifier();
            var classifierResult = (from cl in vContext.Classifier
                                   where cl.IdClassifier == IdClassifier
                                   select cl).First();
            objClassifier =  (Classifier)classifierResult;
            return objClassifier;
        }

        /// <summary>
        /// Obtiene clasificadores segun su ID Tipo
        /// </summary>
        /// <param name="IdClassifierType"></param>
        /// <returns></returns>
        public List<Classifier> GetClassifierByIDType(long IdClassifierType)
        {
            List<Classifier> colClassifier = new List<Classifier>();
            var classifierResult = from cl in vContext.Classifier
                                   where cl.IdClassifierType == IdClassifierType
                                   orderby cl.OrderBy
                                   select cl;
            if (classifierResult != null)
            {
                colClassifier = new List<Classifier>(classifierResult.AsEnumerable<Classifier>());
            }
            return colClassifier;
        }

        public List<Classifier> GetClassifierByIDType(long IdClassifierType, bool isActive)
        {
            List<Classifier> colClassifier = new List<Classifier>();
            var classifierResult = from cl in vContext.Classifier
                                   where cl.IdClassifierType == IdClassifierType && cl.IsActive == isActive
                                   orderby cl.OrderBy
                                   select cl;
            if (classifierResult != null)
            {
                colClassifier = new List<Classifier>(classifierResult.AsEnumerable<Classifier>());
            }
            return colClassifier;
        }

        /// <summary>
        /// Guarda un clasificador.
        /// </summary>
        /// <param name="objClassifier"></param>
        public void SaveClassifier(Classifier objClassifier)
        {
            var validateResult = (from cl in vContext.Classifier
                                   where (cl.Name.CompareTo(objClassifier.Name) == 0 && cl.IdClassifierType == objClassifier.IdClassifierType)
                                   || (cl.Code.CompareTo(objClassifier.Code) == 0 && cl.IdClassifierType == objClassifier.IdClassifierType)
                                   select cl).Count();
            if (validateResult > 0)
            {
                throw new Exception("Ya existe un clasificador con este nombre y/o codigo.");
            }
            
            vContext.AddToClassifier(objClassifier);
            vContext.SaveChanges();
        }


        /// <summary>
        /// Actualiza un clasificador.
        /// </summary>
        /// <param name="objClassifier"></param>
        public void UpdateClassifier(Classifier objClassifier)
        {
            var validateResult = (from cl in vContext.Classifier
                                    where (cl.Name.CompareTo(objClassifier.Name) == 0 && cl.IdClassifierType == objClassifier.IdClassifierType 
                                    && cl.IdClassifier != objClassifier.IdClassifier)
                                    || (cl.Code.CompareTo(objClassifier.Code) == 0 && cl.IdClassifierType == objClassifier.IdClassifierType
                                    && cl.IdClassifier != objClassifier.IdClassifier)
                                    select cl).Count();
            if (validateResult > 0)
            {
                throw new Exception("Ya existe un clasificador con este nombre y/o codigo.");
            }

            var vResult = (from cl in vContext.Classifier
                           where cl.IdClassifier == objClassifier.IdClassifier
                           select cl).First();

            vResult.IdClassifier = objClassifier.IdClassifier;
            vResult.IdClassifierType = objClassifier.IdClassifierType;
            vResult.Name = objClassifier.Name;
            vResult.Code = objClassifier.Code;
            vResult.IsActive = objClassifier.IsActive;
            vResult.OrderBy = objClassifier.OrderBy;

            vContext.SaveChanges();
        }


        /// <summary>
        /// Elimina un clasificador.
        /// </summary>
        /// <param name="idClassifier"></param>
        public void DeleteClassifier(long idClassifier)
        {
            var vResult = (from cl in vContext.Classifier
                           where cl.IdClassifier == idClassifier
                           select cl).First();
            vContext.DeleteObject(vResult);
            vContext.SaveChanges();
        }

        public List<Classifier> GetProductTypeUnused()
        {
            long id = (long)CommonClassifierTypes.ProductType;
            List<Classifier> colClassifier = new List<Classifier>();
            var classifierResult = from cl in vContext.Classifier
                                   where cl.IdClassifierType == id
                                   && !(from c in vContext.CategoryRelation
                                           select c.IdcProductType)
                                    .Contains(cl.IdClassifier)
                                   select cl;
            if (classifierResult != null)
            {
                colClassifier = new List<Classifier>(classifierResult.AsEnumerable<Classifier>());
            }

            return colClassifier;
        }

        public List<Classifier> GetCategoryUnused()
        {
            long id = (long)CommonClassifierTypes.ProductCategory;
            List<Classifier> colClassifier = new List<Classifier>();
            var classifierResult = from cl in vContext.Classifier
                                   where cl.IdClassifierType == id
                                   && !(from c in vContext.CategoryRelation
                                        select c.IdcCategory)
                                    .Contains(cl.IdClassifier)
                                   select cl;
            if (classifierResult != null)
            {
                colClassifier = new List<Classifier>(classifierResult.AsEnumerable<Classifier>());
            }

            return colClassifier;
        }

        public List<Classifier> GetProductTypeByCategory(long idCategory)
        {
            long id = (long)CommonClassifierTypes.ProductType;
            List<Classifier> colClassifier = new List<Classifier>();
            var classifierResult = from cl in vContext.Classifier
                                   where cl.IdClassifierType == id
                                   && (from c in vContext.CategoryRelation
                                       where c.IdcCategory == idCategory
                                        select c.IdcProductType)
                                    .Contains(cl.IdClassifier)
                                   select cl;
            if (classifierResult != null)
            {
                colClassifier = new List<Classifier>(classifierResult.AsEnumerable<Classifier>());
            }

            return colClassifier;
        }
    }
}
